import axios from 'axios'
import { useConversationStore } from '@/store/modules/conversation'
import pinia from '@/store'
const conversationStore = useConversationStore(pinia)

let talk = false

const service = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 600000,
})
axios.defaults.withCredentials = false

//request拦截器
service.interceptors.request.use((config)=>{
    config.headers['Authorization'] = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik1UaEVOVUpHTkVNMVFURTRNMEZCTWpkQ05UZzVNRFUxUlRVd1FVSkRNRU13UmtGRVFrRXpSZyJ9.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL3Byb2ZpbGUiOnsiZW1haWwiOiJncHRAamluaGFpLmVtYWlsIiwiZW1haWxfdmVyaWZpZWQiOnRydWV9LCJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnsicG9pZCI6Im9yZy13QlRyVzNmU3lxSFBmTklBWWVCdlozeUsiLCJ1c2VyX2lkIjoidXNlci1JcXRFS0thTVJ1OXBwY09LOWNtSWY0RUsifSwiaXNzIjoiaHR0cHM6Ly9hdXRoMC5vcGVuYWkuY29tLyIsInN1YiI6ImF1dGgwfDY1ZmNmYWNmY2MzZjMyNjhkMmVhZWY4MCIsImF1ZCI6WyJodHRwczovL2FwaS5vcGVuYWkuY29tL3YxIiwiaHR0cHM6Ly9vcGVuYWkub3BlbmFpLmF1dGgwYXBwLmNvbS91c2VyaW5mbyJdLCJpYXQiOjE3MTIwMjE2MzksImV4cCI6MTcxMjg4NTYzOSwiYXpwIjoiVGRKSWNiZTE2V29USHROOTVueXl3aDVFNHlPbzZJdEciLCJzY29wZSI6Im9wZW5pZCBwcm9maWxlIGVtYWlsIG1vZGVsLnJlYWQgbW9kZWwucmVxdWVzdCBvcmdhbml6YXRpb24ucmVhZCBvcmdhbml6YXRpb24ud3JpdGUgb2ZmbGluZV9hY2Nlc3MifQ.2IQkbP3g6s7J83NY2E7ONUTvRbi-zoyX5iw94pUaEhSJ2_NVLf_XxnlCzPDm4U4qggO_b43EfYhAQvuzxXIBu0zryvvh9UdzLa2QvDRBnRnmksjsEsCiaj33EFP9l1V0He9XlE7-ujTVNwztcGyzvQj-eS-QAk7BRpIL3JlNMhbMGEFTDlkAar-rmshi8vTVmUUytzOIr4_JBDUNUzAxAu94aId1kr8tiurDqTxDYakV7sL0NLGncJLOvZuqjF8psc3qAgqbVW6rWtMOmOXlj8lsf7hGn3ZfXeFxkLCjkBtGAbp9tPqQEiA3h8oOhOzNGrnSmwGuP254c53SNglHTQ'
    config.headers['Content-Type'] = 'application/json;'
    talk = false
    if(config.url.includes('backend-api/conversation') && config.method === 'post'){
        talk = true
        config.headers['accept'] = 'text/event-stream;'
        config.headers['openai-sentinel-chat-requirements-token'] = conversationStore['requirementsToken']
    }
    console.log('config=>',config);
    return config
},(error)=>{
    console.log('error=>',error);
    return Promise.reject(error)
})
//响应拦截器
service.interceptors.response.use((res)=>{
    console.log('res=>',res);
    // return res.data
    if( talk === false){
        return res.data
    }else{
        return res.data
    }
},(error)=>{
    console.log('error=>',error);
    return Promise.reject(error)
})
export default service;